import Vue from 'vue'
import Router from 'vue-router'
import { Notification } from 'element-ui'
import * as path from 'path'

// import Login from './components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
// import Home from './components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Home.vue')
// import Welcome from './components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Welcome.vue')

// import SubsidyInquiry from './components/subsidyApplication/SubsidyInquiry.vue'
const SubsidyInquiry = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/subsidyApplication/SubsidyInquiry.vue')
// import SubsidiesCurrently from './components/subsidyApplication/SubsidiesCurrently.vue'
const SubsidiesCurrently = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/subsidyApplication/SubsidiesCurrently.vue')
// import SubsidiesDetails from './components/subsidyApplication/SubsidiesDetails.vue'
const SubsidiesDetails = () => import(/* webpackChunkName: "Users_Rights_Roles" */ './components/subsidyApplication/SubsidiesDetails.vue')

// import auditList from './components/allowanceAudit/auditList.vue'
const auditList = () => import(/* webpackChunkName: "audit_list" */ './components/allowanceAudit/auditList.vue')
// import auditList from './components/allowanceAudit/auditList.vue'
const auditDetail = () => import(/* webpackChunkName: "audit_list" */ './components/allowanceAudit/auditDetail.vue')

// 查询统计模块路由
const IndexText = () => import(/* webpackChunkName: "IndexText_Funds_Workers" */ './components/query/IndexText.vue')
const Funds = () => import(/* webpackChunkName: "IndexText_Funds_Workers" */ './components/query/Funds.vue')
const Workers = () => import(/* webpackChunkName: "IndexText_Funds_Workers" */ './components/query/Workers.vue')
const Allowance = () => import(/* webpackChunkName: "IndexText_Funds_Workers" */ './components/query/SubsidyInquiry.vue')
const FundsHistory = () => import(/* webpackChunkName: "IndexText_Funds_Workers" */ './components/query/FundsHistory.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/404', component: () => import('@/views/404')},
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/find', component: SubsidyInquiry },
        { path: '/current', component: SubsidiesCurrently },
        { path: '/subsidiesDetails', component: SubsidiesDetails },
        { path: '/indextext', component: IndexText },
        { path: '/funds', component: Funds },
        { path: '/workers', component: Workers },
        { path: '/audit', component: auditList },
        { path: '/audit/detail', component: auditDetail },
        { path: '/allowance', component: Allowance },
        { path: '/funds/fundshistory', component: FundsHistory },
        { path: '/interest', component: () => import('@/components/query/InterestInquiry') }
      ]
    },
    { path: '*', redirect: '/404'}
  ]
})

// 挂载路由导航守卫
const whiteList = ['/404'] // 路由白名单
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('memberToken')
  if (!tokenStr) {
    // 没有token的情况下
    if (whiteList.indexOf(to.path) > -1) {
      // 如果在白名单内，直接放行
      next()
    }  else {
      Notification.error({
        title: '提示 ',
        message: '尚未登录，登录后再使用其它功能！'
      })
      next('/login')
    }
  } else {
    // 有token情况下
    if (to.path === '/login') {
      next('/home')
    } else {
      next()
    }
  }
})

export default router
